// pages/mycenter/index.js
import Dialog from '@vant/weapp/dialog/dialog';
var api = require('../../utils/serve')
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    userId:'',
    show: false,
    shows:false,
    islogin:false,
    usersunm:{
      userdata:"",
      avimg:'',
      useid:'',
      addli:''
    },
    statet:0,
    arrdata:[],
    //总代是否展开
    zondaitrue:true,
    //商学院
    shangis:false,
    //体质
    titrue:false,
    showPayPwdInput: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    payFocus: true, //文本框焦点
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options) {
      var scene = decodeURIComponent(options.scene);
      wx.setStorageSync("scene",scene)
    }
   
//获取userinfo
this.data.userinfo = wx.getStorageSync('userinfo')
// this.data.userinfo = wx.getStorageSync('lo')
console.log(this.data.userinfo);
this.setData({
  token:wx.getStorageSync('token')
})
this.data.userId=wx.getStorageSync('userId')
  console.log(this.data.userId);

if(this.data.token){
  // console.log(this.data.token);
  this.setData({ islogin: true });
}
//获取真实数据渲染信息
wx.request({
  url: app.globalData.url+'/zkxj/user/detail',
  data: {
    "userId": this.data.userId
  },
  method: 'POST',
  header: {
   'content-type': 'application/json',
        'token':wx.getStorageSync('token')
  },
  success:(res)=>{
    wx.setStorageSync('loginuser', res.data.data );
    console.log(res);
    if(res.data.code==0){
      this.setData({ 
        usersunm:{
          avimg:res.data.data.avatarUrl,
          userdata:res.data.data,
          useid:res.data.data.createUserId,
          addli:res.data.data.area,
        }
      })
    }else if(res.data.code==401){
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
    //判断是否展开商学院
 if(this.data.usersunm.userdata.level<5){
    this.setData({
    shangis:true
    })
  }
  }
  })


  //请求计数接口
  wx.request({
  url: app.globalData.url+'/zkxj/order/wechat/count',
  data: {
    "userId": this.data.userId
  },
  method: 'POST',
  header: {
   'content-type': 'application/json',
        'token':wx.getStorageSync('token')
  },
  success:(res)=>{
    console.log(res);
  }
  })
 //请求计数接口
 wx.request({
  url: app.globalData.url+'/zkxj/order/wechat/count',
  data: {
    "userId": this.data.userId
  },
  method: 'POST',
  header: {
   'content-type': 'application/json',
        'token':wx.getStorageSync('token')
  },
  success:(res)=>{
    
    console.log(res);
    this.setData({
      arrdata:res.data.data
    })
   
    
  }
  })
   
  }, 
  //跳转问题解答页面
  handletoquest(){
    wx.navigateTo({
      url: '/pages/question/index',
    })
  },
  handletoword(){
    wx.navigateTo({
      url: '/pages/word/index',
    })
  },
  handletoac(){
    wx.navigateTo({
      url: '/pages/agency/index',
    })
  },
  handleclicode(){
    wx.navigateTo({
          url: '/pages/codepage/index',
        })

  },
   //跳转吸灸案例页面
   goCaseList(){ 
    wx.navigateTo({
      url: '/pages/case/caseList/index',
    })
  },
   //跳转每日打卡页面
   goDiary(){ 
    wx.navigateTo({
      url: '/pages/case/upload/index',
    })
  },
  handletodetectioninfo(){
    wx.navigateTo({
      url: '/pages/bodyteli/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //总代点击切换
  handlecheckout(){
this.setData({
  zondaitrue:!this.data.zondaitrue
})
  },
  //商学院点击
  handlcheckshang(){
    this.setData({
      shangis:!this.data.shangis
    })
  },
  //体质监测点击
  handlcheckti(){
    this.setData({
      titrue:!this.data.titrue
    })
  },
  //点击跳转
  handlegoods(){
wx.navigateTo({
  url: '/pages/one/index',
})
  },
  handletoinformation(){
    wx.navigateTo({
      url: '/pages/infopage/index',
    })
  },
   //点击跳转关于我们
   handleme(){
    wx.navigateTo({
      url: '/pages/two/index',
    })
  },
  //点击进入香灸金
  handletoxiangjiu(){
    wx.navigateTo({
      url: '/pages/moneypage/index',
    })
  },
  
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
    /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    this.setData({
      pwdVal: e.detail.value
    });

    if (e.detail.value.length >= 4) {
      this.hidePayLayer();
    }
  },
  closeLayer(){
    this.setData({
        showPayPwdInput: false
      });
   },
    /**
   * 隐藏支付密码输入层
   */
  hidePayLayer() {
    var val = this.data.pwdVal;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: val
    }, );
    if (this.data.pwdVal) {
        api.get('/zkxj/user/check/identity', {
            endNum: this.data.pwdVal
          }).then(res => {
            if (res.data.code === 0) {
             wx.navigateTo({
                    url: '/pages/financial/index',
                 })
            }else{ 
                wx.showToast({
                     title: res.data.msg,
                    icon: 'error',
                    duration: 2000,
                     mask: true
             })
            }
          })
    }
  },
  //跳转财务管理
  handletopfin(){
    this.setData({ 
      showPayPwdInput: true, 
      pwdVal:''
    })
  
},
  handletojifen(){
    wx.navigateTo({
      url: '/pages/grade/index',
    })
  },
  handletowangt(){
    wx.navigateTo({
      url: '/pages/dictyosome/index',
    })
  },
  handletovideo(){
    wx.navigateTo({
      url: '/pages/vide/index',
    })
  },
  getUserInfoss(event) {
    console.log(event.detail);
    wx.navigateTo({
      url: '/pages/bepromoted/index',
    })
  },
  getUserInfos(event) {
    console.log(this.data.usersunm.userdata.levelName);
    
    if(this.data.usersunm.userdata.levelName=='零售'||this.data.usersunm.userdata.levelName=='会员'){
      wx.navigateTo({
        url: '/pages/bepromoted/index',
      })
    }else{
wx.navigateTo({
      url: '/pages/repurchase/index',
    })
    }
  },
  handletologin(){
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  //跳转复购区
  handletogood(){
    this.setData({ shows: true });
  },
  //个人信息页面
  handletoinfo(){
    wx.navigateTo({
      url: '/pages/info/index',
    })
  },
  //订单页面
  handletoorder(e){
    const statet= e.currentTarget.dataset['state']||0;
    // console.log(statet);
    wx.navigateTo({
      url: `/pages/orderform/index?statet=${statet}`,
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  onCloses() {
    this.setData({ shows: false });
  },

  // 点击弹窗
  handletojin() {
    this.setData({ show: true });
    
  },
  handletuichu() {
    // console.log(1);
    // getApp().globalData.userInfo = {}
    // wx.setStorage({
    //   key: "globalData.userInfo",
    //   data: null

    // })
    // this.setData({
    //   userInfo: null
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     //请求计数接口
 wx.request({
  url: app.globalData.url+'/zkxj/order/wechat/count',
  data: {
    "userId": this.data.userId
  },
  method: 'POST',
  header: {
   'content-type': 'application/json',
        'token':wx.getStorageSync('token')
  },
  success:(res)=>{
    
    console.log(res);
    this.setData({
      arrdata:res.data.data
    })
   
    
  }
  })
    this.setData({
      token:wx.getStorageSync('token')
    })
    console.log(this.data.token);
    // let {
    //   userInfo
    // } = getApp().globalData
    // this.setData({
    //   userInfo: userInfo
    // })
    // console.log(userInfo);

    // getApp().header(`/users/me`,{"X-LC-Session":userInfo.sessionToken},).then(res=>{
    //   console.log(res.data);
    //   this.setData({
    //     userInfo:res.data
    //   }) 
    // })
    //获取真实数据渲染信息
wx.request({
  url: app.globalData.url+'/zkxj/user/detail',
  data: {
    "userId": this.data.userId
  },
  method: 'POST',
  header: {
   'content-type': 'application/json',
        'token':wx.getStorageSync('token')
  },
  success:(res)=>{
    wx.setStorageSync('loginuser', res.data.data );
    console.log(res);
    if(res.data.code==0){

      this.setData({ 
        usersunm:{
          avimg:res.data.data.avatarUrl,
          userdata:res.data.data,
          useid:res.data.data.createUserId,
          addli:res.data.data.area,
        }
      })
    }
    
  }
  })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
       //请求计数接口
 wx.request({
  url: app.globalData.url+'/zkxj/order/wechat/count',
  data: {
    "userId": this.data.userId
  },
  method: 'POST',
  header: {
   'content-type': 'application/json',
        'token':wx.getStorageSync('token')
  },
  success:(res)=>{
    
    console.log(res);
    this.setData({
      arrdata:res.data.data
    })
   
    
  }
  })
    this.setData({
      token:wx.getStorageSync('token')
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      token:wx.getStorageSync('token')
    })
    console.log(this.data.token);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})